//--------------------------------------------------------------------------
// 
//  Copyright (c) Chili Software.  All rights reserved. 
// 
//  File: ObjectContextFactory.cs
//
//  Description: A factory for ObjectContext instances.
// 
//--------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace Chili.Opf3
{
	/// <summary>
	/// Class that represents a factory class for <see cref="ObjectContext">ObjectContext</see> instances. Use the 
	/// <see cref="ObjectContext">ObjectContext</see>'s static Factory property to get an instance.
	/// </summary>
	/// <remarks>
	/// This class represents a factory pattern for creating <see cref="ObjectContext">ObjectContext</see> instances. The class 
	/// requires to set a class that inherits from <see cref="ObjectContextFactoryPolicyBase">ObjectContextFactoryPolicyBase</see>
	/// (Policy property). That class represents a policy for creating new <see cref="ObjectContext">ObjectContext</see> instances.
	/// </remarks>
	/// <example>
	/// This example shows how to set up the factory to create an ObjectContext instance for each new thread.
	/// <code lang="CS">
	/// // Set the policy for multi threading.
	/// ObjectContext.Factory.Policy = new PerThreadObjectContextFactoryPolicy(delegate()
	/// {
	///     MsSqlStorage storage = new MsSqlStorage("... connection string ...");
	///     ObjectContext ctx = new ObjectContext(storage);
	///
	///     return ctx;
	/// });
	/// 
	/// // ... Other code
	/// 
	/// // Have an ObjectContext instance created
	/// ObjectContext context = ObjectContext.Factory.GetObjectContext();
	/// </code>
	/// </example>
	public sealed class ObjectContextFactory
	{
		private ObjectContextFactoryPolicyBase _policy;

		/// <summary>
		/// Creates a new instance of the <see cref="ObjectContextFactory">ObjectContextFactory Class</see>.
		/// </summary>
		internal ObjectContextFactory()
		{
		}

		/// <summary>
		/// Gets an <see cref="ObjectContext">ObjectContext</see> instance. This method might also create a new 
		/// <see cref="ObjectContext">ObjectContext</see> instance, if the policy requires so.
		/// </summary>
		/// <returns>An instance of the <see cref="ObjectContext">ObjectContext</see>.</returns>
		public ObjectContext GetObjectContext()
		{
			if (_policy == null)
				throw new NotSupportedException("No policy set. Please set a policy at the Policy property.");

			return _policy.GetObjectContext();
		}

		/// <summary>
		/// Gets an instance that inherits from <see cref="ObjectContext">ObjectContext</see>. This method might 
		/// also create a new <see cref="ObjectContext">ObjectContext</see> instance, if the policy requires so.
		/// </summary>
		/// <returns>An instance of a class that inherits from <see cref="ObjectContext">ObjectContext</see>.</returns>
		public T GetObjectContext<T>() where T : ObjectContext
		{
			return (T)GetObjectContext();
		}

		/// <summary>
		/// Gets or sets the policy for the <see cref="ObjectContextFactory">ObjectContextFactory</see>.
		/// </summary>
		public ObjectContextFactoryPolicyBase Policy
		{
			get { return _policy; }
			set { _policy = value; }
		}
	}
}
